Playfair Ciphers and PLAYFAIR.EXE Charles Shapiro Compuserve 72300,3632 23 November 1988 Introduction The playfair cipher was invented in 1854 by Charles Wheatstone, the physicist who was also responsible for the measurement tool known as the Wheatstone Bridge. But it bears the name of Lyon Playfair, a British politician who tried to get his foreign office to adopt it. The playfair cipher is a good medium- security cipher system for alphabetic text. You can easily encipher and decipher messages manually with it, and enciphered messages are more secure than they are under a monalphabetic code (such as the Caesar cipher), and easier to encipher and decipher than a book code message. The playfair cipher works on letter pairs; this makes it less vulnerable to solution by frequency analysis, since the most common letter pairs in english (th and he) comprise a much smaller portion of the possible letter pairs than the most common letters do of the possible letters. It also means that a garbled code group affects only two letters of the message you are trying to send. Using the playfair cipher To encipher a message with the playfair cipher, you must first construct the key grid. This grid must be a square; usually, "i" and "j" are equivalent in encoded text so that the key grid is 5 letters on a side. To construct the grid, first think of a key phrase or word with no duplicated letters in it. In our example, we will use the phrase "man bites dog". To construct a playfair grid from the key phrase, first write down the key phrase in 5 letter rows, so: m a n b i t e s d o g Next, fill in the rest of the grid with the letters of the alphabet not found in the phrase: m a n b i t e s d o g c f h k l p q r u v w x y z Next, divide your message into letter pairs, inserting a null letter (such as "x") wherever a letter pair consists of two of the same letter or the message ends without another letter: i see no tourists only soldiers is ex en ot ou ri st so nl ys ol di er sx The second group and the last group in this sample message contain nulls to make encoding it possible. You can find every letter in the message on the code grid. Each pair of letters on the grid can stand in only three relationships to each other: they can share a grid row, they can share a grid column, or they can share neither a row nor a column. To encipher a pair of letters which share a row, write down the grid letter to the right of each member of the pair. To encipher a pair of letters which share a column, write down the grid letter below each member of the pair. In these cases, the grid wraps around; hence, the letter to the right of "i" in the code grid given above is "m". If the pair of letters to encipher share neither a row nor a column, their enciphered equivalents are the letters which are in the same row as the enciphered letter, and the same column as its mate. Hence, to encipher the letter pair "is", you will first find "i" and "s" in the cipher grid, above: m a n b I t e S d o The enciphered letter pair is "no", as follows: m a N b i t e s d O The full enciphered text of the message given above with the key "man bites dog" is as follows: is ex en ot ou ri st so nl ys ol di er sx no sw sa te kz ub de dt mq xd tu ob dp fn To decipher an encoded message, simply reverse the process explained above; take each coded pair of letters, find them in your grid, and look at their relationship. If they share a row, the plaintext letters are to their left; if the share a column, the plaintext letters are above them. If they share neither, the plaintext letter lies in the same row as the code letter and the column of its partner. For added security, you can "transpose" your playfair key before constructing the code grid. To transpose a key, write it out and write the rest of the alphabet beneath it: m a n b i t e s d o g c f h k l p q r u v w x y z Construct your code grid by reading down this primary grid, as follows: m c x a f y n h z b k i l t p e q s r d u o v g w This method eliminates the regular structure of the grid for letters not in your key phrase. Once you have spent about 15 minutes to master this cipher system, you can encipher and decipher quickly with pencil and paper. Playfair.c I have written "PLAYFAIR.C" to automate the process of enciphering and deciphering with the playfair cipher. It is limited in the same ways as the example cipher used above. Enciphered messages contain only lowercase letters; numbers, punctuation, and white space in plaintext are ignored. PLAYFAIR.C takes two arguments: an option string and a key string. The key string must be enclosed by quotes; duplicate letters, spaces, and punctuation marks are automatically removed from it. A "-" must precede the option string. At this writing, there are three option strings: "-p" prints out a copy of the playfair cipher table, "-d" decodes a message with the argument key, and "-t" transposes the argument key as explained above. Options can be combined in logical orders -- "-pt" prints a transposed table, "-dt" decodes using a transposed table. All I/O happens through stdin and stdout. Conclusion I wrote this program for fun and entertainment, so I expect no remuneration for its use. If you make enhancements to the code, please let me know about them. Please note that I will accept no responsibility for the use or misuse of this encipherment method or this program. I also make no specific claims about the security or correctness of the PLAYFAIR.C code or any version of the playfair program.